package com.yjt.termworks;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.makeramen.roundedimageview.RoundedImageView;
import com.yjt.termworks.sql.MyHelper;
import com.yjt.termworks.utils.ImageLoaderUtils;
import com.yjt.termworks.utils.SharePreferenceData;

import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class SendActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_submit,btn_back;
    private EditText sendMsg;
    private TextView send_name;
    private RoundedImageView send_avatar;
    private String baseImageUrl = "http://10.175.253.209:9002/images/userAvatar/";
    private MyHelper myHelper;
    private SQLiteDatabase db;
    private String avatarString;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);

        btn_back = findViewById(R.id.btn_back);
        btn_submit = findViewById(R.id.btn_submit);
        sendMsg = findViewById(R.id.sendMsg);
        send_name = findViewById(R.id.send_name);
        send_avatar = findViewById(R.id.send_avatar);

        btn_submit.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        getDataSQL();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_back:
                back();
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    public void back(){
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("flag", 0);
        startActivity(intent);
    }
    public void submit(){
        myHelper = new MyHelper(this);
        db = myHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        try {
            values.put("username",send_name.getText().toString());
            values.put("avatar",avatarString);
            values.put("likeNum",0);
            values.put("circleMsg",sendMsg.getText().toString());
            values.put("circleImagesList","");
            values.put("circleComment","");
            long id = db.insert("CIRCLE", null, values);
            Log.d("============>",id+"");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.close();

        }

        Toast.makeText(this,"发布成功！",Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("flag", 0);
        startActivity(intent);
    }

    public void getDataSQL(){
         myHelper = new MyHelper(this);
         db = myHelper.getWritableDatabase();

        Map<String, String> userInfo = SharePreferenceData.getUserData(this);
        String userPhone = userInfo.get("userName");
        Cursor cursor = db.rawQuery("select * from USER where phone = ?", new String[]{userPhone});
        try {
            if (cursor.moveToFirst()) {
                avatarString = cursor.getString(4);
                send_name.setText(cursor.getString(1));
                ImageLoaderUtils.display(this,send_avatar,baseImageUrl+cursor.getString(4));
            } else {
                Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
    }
}